package controler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import model.Academy;
import model.Subject;
import model.Teacher;
import model.beans.PlanEstudioBean;
import business.misc.Util;

public class StudyPlan extends MappingDispatchAction {
	public ActionForward academies(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	request.setAttribute("lst_academia", Academy.list(Validate.user_log_t.getId()));
    	HttpSession session = request.getSession();
    	String str_source = request.getParameter("source");
    	String source;
    	if( str_source != null ){
    		source = request.getParameter("source");
    	}else{
    		source = (String)session.getAttribute("session_source");
    	}
    	String title = null;
    	MessageResources msgRs = this.getResources(request);
    	if(source.equals("draft")){
    		title = msgRs.getMessage("Menu.Draft");
    	}else{
    		title = msgRs.getMessage("Menu.StudyPlan");
    	}
    	request.setAttribute("title", title);
    	session.setAttribute("session_source",source );
		return mapping.findForward(Util.DEFAULT);
	}

	public ActionForward subjects(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	String str_academyCode = request.getParameter("academy_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("lst_materia", Subject.list(str_academyCode));
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward contents(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	HttpSession session = request.getSession(); 
    	session.setAttribute("inst_contenido", model.StudyPlan.instance(str_subjectCode));
    	session.setAttribute("inst_bibliografia", model.Bibliography.list(str_academyCode,str_subjectCode));
    	
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward generalGoal(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	
    	DynaActionForm forma = (DynaActionForm)form;
    	PlanEstudioBean plan = (PlanEstudioBean)request.getSession().getAttribute("inst_contenido");
    	forma.set("propositoGeneral", plan.getProposito()!=null ? plan.getProposito() : "" );
    	
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward save_generalGoal(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	
    	DynaActionForm forma = (DynaActionForm)form;
    	String str_proposito = forma.getString("propositoGeneral");
    	model.StudyPlan.saveGeneralGoal(str_subjectCode, str_proposito);
    	PlanEstudioBean plan = (PlanEstudioBean)request.getSession().getAttribute("inst_contenido");
    	plan.setProposito(str_proposito);
    	request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessEdit"));
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward objEnd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	
    	DynaActionForm forma = (DynaActionForm)form;
    	PlanEstudioBean plan = (PlanEstudioBean)request.getSession().getAttribute("inst_contenido");
    	forma.set("objetivoTerminal", plan.getObjetivoTerminal()!=null ? plan.getObjetivoTerminal() : "" );
    	forma.set("objetivoIntelectual", plan.getObjectivoIntelectual()!=null ? plan.getObjectivoIntelectual() : "" );
    	forma.set("objetivoSocioHumano", plan.getObjectivoSocialHumano()!=null ? plan.getObjectivoSocialHumano() : "" );
    	forma.set("objetivoProfesional", plan.getObjectivoProfesional()!=null ? plan.getObjectivoProfesional() : "" );
    	
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward save_objEnd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	
    	DynaActionForm forma = (DynaActionForm)form;
    	String str_terminal = forma.getString("objetivoTerminal");
    	String str_intelectual = forma.getString("objetivoIntelectual");
    	String str_socioHumano = forma.getString("objetivoSocioHumano");
    	String str_profesional = forma.getString("objetivoProfesional");
    	
    	model.StudyPlan.saveObjEnd(str_subjectCode, str_terminal, str_intelectual, str_socioHumano, str_profesional);
    	PlanEstudioBean plan = (PlanEstudioBean)request.getSession().getAttribute("inst_contenido");
    	plan.setObjectivoIntelectual(str_intelectual);
    	plan.setObjectivoProfesional(str_profesional);
    	plan.setObjectivoSocialHumano(str_socioHumano);
    	plan.setObjetivoTerminal(str_terminal);
    	
    	request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessEdit"));
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward conceptRelation(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	
    	DynaActionForm forma = (DynaActionForm)form;
    	PlanEstudioBean plan = (PlanEstudioBean)request.getSession().getAttribute("inst_contenido");
    	forma.set("conceptosPrevios", plan.getConceptosPrevios()!=null ? plan.getConceptosPrevios() : "" );
    	forma.set("conceptosPosteriores", plan.getConceptosPosteriores()!=null ? plan.getConceptosPosteriores() : "" );
    	
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward save_conceptRelation(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	
    	DynaActionForm forma = (DynaActionForm)form;
    	String str_previos = forma.getString("conceptosPrevios");
    	String str_posteriores = forma.getString("conceptosPosteriores");
    	
    	model.StudyPlan.saveConceptos(str_subjectCode, str_previos, str_posteriores);
    	PlanEstudioBean plan = (PlanEstudioBean)request.getSession().getAttribute("inst_contenido");
    	plan.setConceptosPrevios(str_previos);
    	plan.setConceptosPosteriores(str_posteriores);
    	
    	request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessEdit"));
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward setting(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	
    	DynaActionForm forma = (DynaActionForm)form;
    	PlanEstudioBean plan = (PlanEstudioBean)request.getSession().getAttribute("inst_contenido");
    	forma.set("encuadre", plan.getEncuadre()!=null ? plan.getEncuadre() : "" );
    	forma.set("tiempoEncuadre", Integer.toString(plan.getTiempoEncuadre()));
    	
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward save_setting(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	
    	DynaActionForm forma = (DynaActionForm)form;
    	String str_encuadre = forma.getString("encuadre");
    	int int_tiempo = forma.getString("tiempoEncuadre")!=null && !forma.getString("tiempoEncuadre").equals("")? Integer.parseInt(forma.getString("tiempoEncuadre")) : 0;
    	
    	model.StudyPlan.saveEncuadre(str_subjectCode, str_encuadre, int_tiempo);
    	PlanEstudioBean plan = (PlanEstudioBean)request.getSession().getAttribute("inst_contenido");
    	plan.setEncuadre(str_encuadre);
    	plan.setTiempoEncuadre(int_tiempo);
    	
    	request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessEdit"));
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward scoredCriterial(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	
    	DynaActionForm forma = (DynaActionForm)form;
    	PlanEstudioBean plan = (PlanEstudioBean)request.getSession().getAttribute("inst_contenido");
    	forma.set("criteriosCalificacion", plan.getCriteriosCalificacion()!=null ? plan.getCriteriosCalificacion() : "" );
    	
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward save_scoredCriterial(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	
    	DynaActionForm forma = (DynaActionForm)form;
    	String str_criterios = forma.getString("criteriosCalificacion");
    	
    	model.StudyPlan.saveCriteriosCalificacion(str_subjectCode, str_criterios);
    	PlanEstudioBean plan = (PlanEstudioBean)request.getSession().getAttribute("inst_contenido");
    	plan.setCriteriosCalificacion(str_criterios);
    	
    	request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessEdit"));
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward teachers(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	request.setAttribute("lst_maestro", Teacher.list(str_subjectCode));
		return mapping.findForward(Util.DEFAULT);
	}
}
